package pers.cl.dubbo.gateway.channel;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import org.springframework.context.ApplicationContext;
import pers.cl.dubbo.gateway.channel.handler.HttpRequestHandler;
import pers.cl.dubbo.mvc.DispatchServlet;

/**
 * @author chenlei
 * @date 2020-01-07
 * 初始化channel
 * @see HttpRequestHandler
**/
public class HttpServerInitializer extends ChannelInitializer<SocketChannel>{

	private ApplicationContext context;

	public HttpServerInitializer(ApplicationContext context) {
		this.context = context;
	}

	/*
	 * @author chenlei
	 * @date 2020-01-07
	 * @description 
	 * @see io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.Channel)
	 */
	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new HttpServerCodec());
		pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024));
		pipeline.addLast(new HttpRequestHandler(new DispatchServlet(context)));
	}

}
